from django.apps import AppConfig
from .mqtt_client import MqttPublisher  # 导入你的MQTT工具类
import os

class MqttConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'mqtt'


    # 定义全局MQTT客户端实例（类属性，整个应用生命周期内唯一）
    mqtt_publisher = None

    def ready(self):
        """应用启动时执行（仅在Django启动时调用一次）"""
        # 初始化MQTT客户端并连接
        print(f"ready() 被调用，进程ID: {os.getpid()}")
        self.mqtt_publisher = MqttPublisher()
        self.mqtt_publisher.connect()
        print("MQTT客户端已初始化并连接")